package com.tools.cleanmaster.utils;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;

import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.appinfo.AppManagerCompat;
import com.tools.cleanmaster.store.GlobalConfigSharedPrefManager;

public class SignatureChecker {
    private static final String TAG = "SignatureChecker";
    private static final boolean DEBUG = FeatureConfig.DEBUG_LOG;

    /** MD5 checksum of OPDA release key */
    public static final String SIGNATURE_CHECKSUM_OPDAR = "2wioi4tb9idxctczzhx8jk2pa";
    /** MD5 checksum of Tapas release key */
    public static final String SIGNATURE_CHECKSUM_DXR = "3ya3kmvchr0sy572vwothpb3c";
    /** MD5 checksum of OPDA debug key */
    public static final String SIGNATURE_CHECKSUM_OPDAD = "4xdm7oblwioop9e9sihzdiceo";
    /** MD5 checksum of Tapas debug key */
    public static final String SIGNATURE_CHECKSUM_DXD = "6q2f72pbvsundumlbh8qo54ta";

    /** Signature tag for OPDA release key */
    public static final String SIGNATURE_TAG_OPDAR = "r1";
    /** Signature tag for DX release key */
    public static final String SIGNATURE_TAG_DXR = "r2";
    /** Signature tag for OPDA debug key */
    public static final String SIGNATURE_TAG_OPDAD = "d1";
    /** Signature tag for DX debug key */
    public static final String SIGNATURE_TAG_DXD = "d2";

    public static String getSignatureTag(Context cxt) {
        String signatureTag = GlobalConfigSharedPrefManager.getSignatureTag(cxt);
        if (signatureTag != null) {
            return signatureTag;
        }

        try {
            Signature[] signatures = AppManagerCompat.getAppInfo(CleanConstant.REAL_PACKAGE_NAME).getSignatures();
            if (signatures != null && signatures.length > 0) {
                // Only check the first signature
                String checksum = CodecUtils.getMD5Checksum(signatures[0].toByteArray());
                if (DEBUG) {
                    LogHelper.d(TAG, "signature checksum: " + checksum);
                }
                if (checksum != null) {
                    if (SIGNATURE_CHECKSUM_OPDAR.equals(checksum)) {
                        signatureTag = SIGNATURE_TAG_OPDAR;
                    } else if (SIGNATURE_CHECKSUM_DXR.equals(checksum)) {
                        signatureTag = SIGNATURE_TAG_DXR;
                    } else if (SIGNATURE_CHECKSUM_OPDAD.equals(checksum)) {
                        signatureTag = SIGNATURE_TAG_OPDAD;
                    } else if (SIGNATURE_CHECKSUM_DXD.equals(checksum)) {
                        signatureTag = SIGNATURE_TAG_DXD;
                    } else {
                        signatureTag = checksum.substring(0, 5);
                    }
                    GlobalConfigSharedPrefManager.saveSignatureTag(cxt, signatureTag);
                }
            }
        } catch (NameNotFoundException e) {
            // ignore the exception
        }
        return signatureTag;
    }
}
